package com.jiangyao.controller;


import com.jiangyao.common.ApiResult;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.common.utils.StringUtils;
import com.jiangyao.common.utils.Util;
import com.jiangyao.model.dto.CollectDto;
import com.jiangyao.model.entity.ApiAccessLog;
import com.jiangyao.model.entity.Collect;
import com.jiangyao.service.ApiAccessLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.jiangyao.controller.BaseController;

import java.util.Date;

/**
 * <p>
 * 访问日志表 前端控制器
 * </p>
 *
 * @author xiangyan
 * @since 2020-09-07
 */
@RestController
@RequestMapping("/apiAccessLog")
@Api(tags = "访问日志")
public class ApiAccessLogController extends BaseController {

    @Autowired
    ApiAccessLogService service;
    @PostMapping("/save")
    @ApiOperation(value = "写入访问日志 必传：accessUrl")
    public ApiResult<Boolean> saveApiAccessLog(@RequestBody ApiAccessLog apiAccessLog){
        if(StringUtils.isBlank(apiAccessLog.getAccessUrl())){
            throw new ServerException(306, "需必传accessUrl");
        }
        apiAccessLog.setCreateTime(new Date());
        apiAccessLog.setToken(getLoginToken());
        apiAccessLog.setUserId(getLoginSessionUserId());
        apiAccessLog.setIp(Util.getIpAddr());
        service.save(apiAccessLog);
        return ApiResult.success(true);
    }
}

